﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "QTreeWidgetPaper.h"
#include "IUiView.h"
#include "GbmpNew.h"
#include "IModelView.h"
#include "UniIdentity.h"
#include "IDrawingLayout.h"
#include "IDrawingLayoutManager.h"
#include "IUiDocumentViewManager.h"
#include "IElementBasicInformation.h"

#include "CommandIds.h"
#include "ModelViewDlg.h"
#include <QTreeWidgetItem>
#include "UiLayoutManager.h"
#include "QFamilyTreeWidgetUtil.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

QTreeWidgetPaper::QTreeWidgetPaper()
{}

QTreeWidgetPaper::~QTreeWidgetPaper() {}

void QTreeWidgetPaper::UpdatePaperItems(QTreeWidgetItem *pPaperRootItem, gcmp::IDocument* pDoc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pPaperRootItem, L"pPaperRootItem指针为空",L"GDMP",L"2023-10-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc指针为空",L"GDMP",L"2023-10-20");

    //清除之前添加的的子节点
    //QList<QTreeWidgetItem*> children = pPaperRootItem->takeChildren();
    //if (children.size())
    //{
    //    children.clear();
    //}
    DELETE_QTREEWIDGET_CHILDREN(pPaperRootItem);
    //FOR_EACH(paperItem, children)
    //{
    //    QList<QTreeWidgetItem*> viewportItems = paperItem->takeChildren();
    //    FOR_EACH(viewportItem, viewportItems)
    //    {
    //        /*OwnerPtr<QTreeWidgetItem> opItem(viewportItem);
    //        paperItem->removeChild(viewportItem);*/
    //    }
    //    /*OwnerPtr<QTreeWidgetItem> opItem(paperItem);
    //    paperItem->removeChild(paperItem);*/
    //}

    std::wstring errorMessage = L"";
    const IDrawingLayoutManager* pLayoutsManager = IDrawingLayoutManager::Get(pDoc, &errorMessage);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pLayoutsManager, L"pLayoutsManager为空",L"GDMP",L"2023-10-20");
    std::vector<IDrawingLayout*> layouts = pLayoutsManager->GetAllLayouts();
    pPaperRootItem->setExpanded(true);
    ModelViewDlg* pModelViewDlg = dynamic_cast<ModelViewDlg*>(IModelViewDlg::Get(DOCK_PAGE_NAME_VIEW_MANAGEMENT));
    UiLayoutManager* pUiLayoutManager = nullptr;
    if (pModelViewDlg)
    {
        pUiLayoutManager = pModelViewDlg->GetUiLayoutManager();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiLayoutManager, L"pUiLayoutManager为空",L"GDMP",L"2023-10-20");
    }

    for (int i = 0; i < layouts.size(); ++i)
    {
        std::wstring paperName = layouts[i]->GetName();
        OwnerPtr<QTreeWidgetItem> childItem = NEW_AS_OWNER_PTR(QTreeWidgetItem, pPaperRootItem, QStringList(QString::fromStdWString(paperName)));
        //std::wstring  debugstring = layouts[i]->GetOwnerElement()->GetBasicInformation()->GetCategoryUid().GetDebugString();
        childItem->setData(0, PAPER_USER_DATA_ROLE, QString::fromStdWString(layouts[i]->GetOwnerElement()->GetBasicInformation()->GetCategoryUid().GetDebugString()));
        childItem->setExpanded(true);
        IDrawingLayout* pCurLayout = pUiLayoutManager != nullptr ? pUiLayoutManager->GetCurrentLayout(pDoc) : nullptr;
        if (pCurLayout && pCurLayout->GetName() == paperName)
        {
            IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMP",L"2023-10-20");
            IUiView* pCurrentView = pUiDocViewMgr->GetCurrentUiView();
            if (pCurrentView != nullptr && paperName == pCurrentView->GetModelView()->GetName())
            {
                pModelViewDlg->HighlightSelectedModelView(pCurrentView);
            }
        }
        std::vector<IDrawingViewport*> viewPorts = layouts[i]->GetAllViewports();
        for (int j = 0; j<viewPorts.size(); j++)
        {
            std::wstring viewPortName = viewPorts[j]->GetName();
            OwnerPtr<QTreeWidgetItem> viewPortItem = NEW_AS_OWNER_PTR(QTreeWidgetItem, childItem.get(), QStringList(QString::fromStdWString(viewPortName)));
            //std::wstring  viewdebugstring = viewPorts[j]->GetOwnerElement()->GetBasicInformation()->GetCategoryUid().GetDebugString();
            viewPortItem->setData(0, PAPER_USER_DATA_ROLE, QString::fromStdWString(viewPorts[j]->GetOwnerElement()->GetBasicInformation()->GetCategoryUid().GetDebugString()));
            IDrawingViewport* pCurViewport = pUiLayoutManager != nullptr ? pUiLayoutManager->GetCurrentViewport(pDoc) : nullptr;
            if (pCurViewport && viewPortName == pCurViewport->GetName())
            {
                pModelViewDlg->HighLightSelectedViewport(viewPortItem.get());
            }
            childItem->addChild(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(viewPortItem));
        }
        pPaperRootItem->addChild(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(childItem));
    }
}

void QTreeWidgetPaper::UpdateViewPortItem(QTreeWidgetItem *pSelectedRootItem, IDrawingViewport *pViewPort)
{
    std::wstring strName = pViewPort->GetName();
    OwnerPtr<QTreeWidgetItem> childItemView = NEW_AS_OWNER_PTR(QTreeWidgetItem, pSelectedRootItem, QStringList(QString::fromStdWString(strName)));
    childItemView->setData(0, PAPER_USER_DATA_ROLE, QString::fromStdWString(pViewPort->GetOwnerElement()->GetBasicInformation()->GetCategoryUid().GetDebugString()));
    pSelectedRootItem->addChild(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(childItemView));
}

//QTreeWidgetItem* QTreeWidgetPaper::AddPaperItem(QTreeWidgetItem* pParent, const QString& paperName)
//{
//    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pParent, L"pParent指针为空", L"GDMP",L"2023-10-20");
//    OwnerPtr<QTreeWidgetItem> childItem = NEW_AS_OWNER_PTR(QTreeWidgetItem, pParent, QStringList(paperName)); 
//    pParent->addChild(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(childItem));
//    return childItem.get();
//}

QTreeWidgetItem* QTreeWidgetPaper::FindChildWidgetItem(const std::wstring& itemName)
{
    ModelViewDlg* pModelViewDlg = dynamic_cast<ModelViewDlg*>(IModelViewDlg::Get(DOCK_PAGE_NAME_VIEW_MANAGEMENT));
    if (!pModelViewDlg)
    {
        return nullptr;
    }
    QTreeWidgetItem* pRootPaper = pModelViewDlg->GetRootPaperItem();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pRootPaper, L"pRootPaper为空",L"GDMP",L"2023-10-20");
    for (int i = 0; i < pRootPaper->childCount(); ++i)
    {
        QTreeWidgetItem* pPaperItem = pRootPaper->child(i);
        if (pPaperItem && pPaperItem->text(0).toStdWString() == itemName)
        {
            return pPaperItem;
        }
        else if (pPaperItem)
        {
            for (int j = 0; j < pPaperItem->childCount(); ++j)
            {
                QTreeWidgetItem* pViewportItem = pPaperItem->child(j);
                if (pViewportItem && pViewportItem->text(0).toStdWString() == itemName)
                {
                    return pViewportItem;
                }
            }
        }
    }
    return nullptr;
}

